#include "register.h"
#include "ui_register.h"
#include <QFileDialog>
#include <qfaceobject.h>
#include <QSqlTableModel>
#include <QSqlRecord>
#include <QMessageBox>
#include <QDebug>



Register::Register(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);
}

Register::~Register()
{
    delete ui;
}

void Register::timerEvent(QTimerEvent *e)
{

    // 获取摄像头数据并且显示在界面上 时时刷新
    if(cap.isOpened())
    {
        cap>>image;
        if(image.data == nullptr) return;
    }

    // Mat转为 QImage

    cv::cvtColor(image,rgbImage,cv::COLOR_BGR2RGB);
    QImage qImg(rgbImage.data,rgbImage.cols,rgbImage.rows,rgbImage.step1(),QImage::Format_RGB888);

    // 在QT界面上显示
    QPixmap mmp = QPixmap::fromImage(qImg);

    mmp = mmp.scaledToWidth(ui->headpicLb->width());
    ui->headpicLb->setPixmap(mmp);

}

void Register::on_resetbt_clicked()
{
    // 清空数据
    ui->nameEdit->clear();
    ui->birthdayEdit->setDate(QDate::currentDate());
    ui->addressEdit->clear();
    ui->phoneEdit->clear();
    ui->picFileEdit->clear();
}

void Register::on_addpicBt_clicked()
{
    // 文件对话框选择图片路径
    QString filepath = QFileDialog::getOpenFileName(this);
    ui->picFileEdit->setText(filepath);

    // 显示图片
    QPixmap mmp(filepath);
    mmp = mmp.scaledToWidth(ui->headpicLb->width());
    ui->headpicLb->setPixmap(mmp);


}

void Register::on_registerbt_clicked()
{
    // 通过照片，结合faceobject模块得到faceID
    QFaceObject faceobj;
    cv::Mat image = cv::imread(ui->picFileEdit->text().toUtf8().data());
    int faceID = faceobj.face_register(image);
    qDebug()<<faceID;

      // 头像保存在固定路径下
    QString headfile = QString("./data/%1.jpg").arg(QString(ui->nameEdit->text().toUtf8().toBase64()));
    cv::imwrite(headfile.toUtf8().data(),image);


    // 把个人信息存储到数据库employee
    QSqlTableModel model;
    model.setTable("employee");     // 数据库表名
    QSqlRecord record = model.record();

    // 设置数据
    record.setValue("name",ui->nameEdit->text());
    record.setValue("sex",ui->mrb->isChecked()?"男":"女");
    record.setValue("birthday",ui->birthdayEdit->text());
    record.setValue("address",ui->addressEdit->text());
    record.setValue("phone",ui->phoneEdit->text());
    record.setValue("faceID",faceID);   // 图片路径
    record.setValue("headfile",headfile);


    bool ret = model.insertRecord(0,record);// 记录插入到数据库中
    if(ret)
    {
        QMessageBox::information(this,"注册提示","注册成功");
        model.submitAll();
    }else
    {
        QMessageBox::information(this,"注册提示","注册失败");
    }

}

void Register::on_videoswitchBt_clicked()
{

    if(ui->videoswitchBt->text() == "打开摄像头")
    {
        if(cap.open(0))
        {
            ui->videoswitchBt->setText("关闭摄像头");
            timerid = startTimer(1000);  // 启动定时器事件

        }
    }else
    {
        killTimer(timerid);
        ui->videoswitchBt->setText("打开摄像头");
        cap.release();  //关闭摄像头
    }
}

void Register::on_cameraBt_clicked()        // 拍照
{
    QString headfile = QString("./data/%1.jpg").arg(QString(ui->nameEdit->text().toUtf8().toBase64()));
    ui->picFileEdit->setText(headfile);

    cv::imwrite(headfile.toUtf8().data(),image);    // 保存图片

    killTimer(timerid);
    ui->videoswitchBt->setText("打开摄像头");
    cap.release();  //关闭摄像头
}
